﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace KaKaSongGame
{
    public partial class ActionTimer : UserControl
    {
        DispatcherTimer _timer;
        public ActionTimer()
        {
            InitializeComponent();
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += new EventHandler(_timer_Tick);
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            secremain--;

            tb1.Text = secremain.ToString();

            if (secremain == 0)
            {
                sb1.Stop();
                _timer.Stop();
                actiontimerhidesb1.Begin();
                if (_oncomplted != null)
                {
                    _oncomplted();
                    _oncomplted = null;
                }
            }
        }

        int secremain;
        Action _oncomplted;
        public void ActiveTimer(TimeSpan secs,Action oncompleted=null)
        {
            // finished the last active action.
            if (_oncomplted != null)
                _oncomplted();

            sb1.Begin();
            actiontimershowsb1.Begin();

            _timer.Start();
            secremain = (int)secs.TotalSeconds;
            tb1.Text = secremain.ToString();
            _oncomplted = oncompleted;
        }

        public void DeactiveTimer(bool trigger)
        {
            if (trigger && _oncomplted!=null)
            {
                _oncomplted();
            }
            actiontimerhidesb1.Begin();
            _timer.Stop();
            _oncomplted = null;
        }
    }
}
